package ph.gov.marikina.mcsmtms.tests;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Roads;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.SituationTypes;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.TrafficConditionTypes;
import ph.gov.marikina.mcsmtms.domain.MobileUserReport;
import ph.gov.marikina.mcsmtms.domain.SituationReport;
import ph.gov.marikina.mcsmtms.rest.webservices.client.BasicSecureSimpleClientHttpRequestFactory;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCredentials;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCrendentialsProvider;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;


public class SituationReportControllerTest {

	Logger log = Logger.getLogger(SituationReportControllerTest.class);

	/** The context. */
	// private static ApplicationContext springContext = new
	// ClassPathXmlApplicationContext(
	// "spring/test-spring-config.xml");

//	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
//			"test-applicationContext.xml");

	private static final String SITUATION_REPORT_REST_SERVICE_URL = "http://mcsmtms-ws.appspot.com/rest/hazardmap/";
	private static final String HAZARD_MAP_REST_SERVICE_URL = "http://mcsmtms-ws.appspot.com/rest/hazardmap/B_TO_A";

	private RestTemplate restTemplate;

	/** The application context. */
	// private static ApplicationContext applicationContext = new
	// ClassPathXmlApplicationContext(
	// "applicationContext.xml");

	@Before
	public void setUp() throws Exception {
		restTemplate = new RestTemplate();
	}

	@Test
	public void listAllSituationReportsTest() {
		
		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(new SimpleCredentials("mmda:1", "enrico")));
		restTemplate.setRequestFactory(factory);
		String result = restTemplate.getForObject(SITUATION_REPORT_REST_SERVICE_URL, String.class);
		
        //System.out.println(result);
        
        Gson gson = new Gson();
        JsonElement jelement = new JsonParser().parse(result);
        JsonObject jobject = jelement.getAsJsonObject();
        List<SituationReport> situationReportList = gson.fromJson(jobject.getAsJsonArray("hazardMapList"), new TypeToken<List<SituationReport>>(){}.getType());
        
	    for (SituationReport situationReport : situationReportList ) {
	    	System.out.println(situationReport.getId() + " " + situationReport.getRoad() + " " + situationReport.getDirection());
	    }
        
	}
	
	@Ignore
	public void listHazardMapReportsForDirection() {
		
		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(new SimpleCredentials("mmda:1", "enrico")));
		restTemplate.setRequestFactory(factory);
		String result = restTemplate.getForObject(HAZARD_MAP_REST_SERVICE_URL, String.class);
		
        //System.out.println(result);
        
        Gson gson = new Gson();
        JsonElement jelement = new JsonParser().parse(result);
        JsonObject jobject = jelement.getAsJsonObject();
        List<SituationReport> situationReportList = gson.fromJson(jobject.getAsJsonArray("hazardMapList"), new TypeToken<List<SituationReport>>(){}.getType());
        
	    for (SituationReport situationReport : situationReportList ) {
	    	System.out.println(situationReport.getId() + " " + situationReport.getRoad());
	    }
        
	}
	
//	public void approveReportTest() {
//
//		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
//		factory.setCredentialsProvider(new SimpleCrendentialsProvider(
//				new SimpleCredentials("rix:1", "enrico")));
//
//		Gson gson = new Gson();
//	
//		HttpHeaders headers = new HttpHeaders();  
//        headers.setContentType( MediaType.APPLICATION_JSON );
//        
//        String jsonData = gson.toJson(40);
//        
//        System.out.println("jsonData: " + jsonData);
//  
//        HttpEntity<String> request= new HttpEntity<String>(jsonData, headers);
//		
//		restTemplate.setRequestFactory(factory);
//
//		String result = restTemplate.postForObject(MOBILE_USER_REPORT_REST_SERVICE_URL, mobileUserReport, String.class);
//				// getForObject(SITUATION_REPORT_REST_SERVICE_URL,
////				postForObject(
////				MOBILE_USER_REPORT_REST_SERVICE_URL + "create/", request,
////				String.class);// getForObject(SITUATION_REPORT_REST_SERVICE_URL,
//								// String.class);
//		
//        JsonElement jElement = new JsonParser().parse(result);
//        JsonObject jObject = jElement.getAsJsonObject();
//        mobileUserReport = gson.fromJson(jObject.get("mobileUserReport"), new TypeToken<MobileUserReport>(){}.getType());
//        
//        Assert.assertNotNull(mobileUserReport);
//        System.out.println(mobileUserReport.getId());
//	}
}

